/**
 * Copyright 2004 - 2007 Blue Bamboo International Inc. 
 *           All rights reserved.
 *
 *
 * Blue Bamboo PROPRIETARY/CONFIDENTIAL.
 *
 */

package com.bambamboo.st.socket.server.mina;

import java.net.InetSocketAddress;
import java.util.Map.Entry;

import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.jboss.netty.util.internal.ConversionUtil;

import com.bambamboo.st.AbstractIOServer;
import com.bambamboo.st.socket.server.SocketConnectionBean;

/**
 * TCP Server implemented with Apach MINA.
 * <p>
 * Binding socket port and delegate all request to <code>IProecess</code>
 * </p>
 * 
 * @author Nick.Tan
 * @since 1.0.0
 */
public class MinaTCPServer extends AbstractIOServer {

    private AbstractIoHandler ioHandler;

    @Override
    public void startup() throws Exception {
        SocketAcceptor acceptor = new NioSocketAcceptor();
        SocketConnectionBean bean = (SocketConnectionBean) getConnectionBean();
        AbstractIoHandler iohandler = new DefaultIoHandler();
        iohandler.setConfigurationBean(bean);
        acceptor.setHandler(iohandler);

        SocketSessionConfig socketSessionConfig = acceptor.getSessionConfig();
        for (Entry entry : bean.getOptions().entrySet()) {
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (key.equals("receiveBufferSize")) {
                socketSessionConfig.setReceiveBufferSize(ConversionUtil.toInt(value));
            } else if (key.equals("sendBufferSize")) {
                socketSessionConfig.setSendBufferSize(ConversionUtil.toInt(value));
            } else if (key.equals("tcpNoDelay")) {
                socketSessionConfig.setTcpNoDelay(ConversionUtil.toBoolean(value));
            } else if (key.equals("keepAlive")) {
                socketSessionConfig.setKeepAlive(ConversionUtil.toBoolean(value));
            } else if (key.equals("reuseAddress")) {
                socketSessionConfig.setReuseAddress(ConversionUtil.toBoolean(value));
            } else if (key.equals("soLinger")) {
                socketSessionConfig.setSoLinger(ConversionUtil.toInt(value));
            } else if (key.equals("trafficClass")) {
                socketSessionConfig.setTrafficClass(ConversionUtil.toInt(value));
            }
        }

        String port = bean.getPort();
        acceptor.bind(new InetSocketAddress(Integer.parseInt(port)));
    }

    @Override
    public String getType() {
        return "TCP";
    }

    /**
     * @return the ioHandler
     */
    public AbstractIoHandler getIoHandler() {
        return ioHandler;
    }

    /**
     * @param ioHandler the ioHandler to set
     */
    public void setIoHandler(AbstractIoHandler ioHandler) {
        this.ioHandler = ioHandler;
    }

}
